﻿using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;

[RequireComponent(typeof(BoxCollider2D))]
public class Repeater : MonoBehaviour
{
    [CompilerGenerated]
    private static Action<Transform> <>f__am$cache9;
    private bool inited;
    private Queue<Transform> instances = new Queue<Transform>();
    private Vector2 lastSize;
    [SerializeField]
    private Vector2 m_direction = Vector2.right;
    [SerializeField]
    private Transform m_prefab;
    private BoxCollider2D m_trigger;
    private Transform m_tsm;
    public Transform prefabs;

    public event Action<Transform> onRepeat;

    public Repeater()
    {
        if (<>f__am$cache9 == null)
        {
            <>f__am$cache9 = new Action<Transform>(Repeater.<onRepeat>m__154);
        }
        this.onRepeat = <>f__am$cache9;
    }

    [CompilerGenerated]
    private static void <onRepeat>m__154(Transform instance)
    {
    }

    public static Repeater Create(Transform prefab, Vector2 direction, Vector2 center, Vector2 size)
    {
        return new GameObject(typeof(Repeater).Name).AddComponent<Repeater>().Init(prefab, direction, center, size);
    }

    private BoxCollider2D GetTrigger()
    {
        if (this.m_trigger == null)
        {
            this.m_trigger = base.gameObject.AddComponent<BoxCollider2D>();
            this.m_trigger.isTrigger = true;
        }
        return this.m_trigger;
    }

    private Repeater Init(Transform prefab, Vector2 direction, Vector2 center, Vector2 size)
    {
        this.direction = direction;
        this.prefab = prefab;
        this.center = (Vector3) center;
        this.size = size;
        this.lastSize = size;
        this.InstantiatePrefab();
        this.inited = true;
        return this;
    }

    private Transform InstantiatePrefab()
    {
        Transform item = (Transform) UnityEngine.Object.Instantiate(this.prefab, base.transform.position, base.transform.rotation);
        this.instances.Enqueue(item);
        return item;
    }

    public Transform MoveNext()
    {
        this.tsm.position = this.tsm.TransformPoint(Vector3.Scale((Vector3) this.direction, (Vector3) ((this.lastSize / 2f) + (this.size / 2f))));
        Transform transform = this.InstantiatePrefab();
        this.lastSize = this.size;
        return transform;
    }

    private void OnDrawGizmos()
    {
        Gizmos.color = (Color) (Color.white * 0.5f);
        Gizmos.DrawWireCube(this.center, (Vector3) this.size);
    }

    private void OnTriggerEnter2D(Collider2D co)
    {
        if (this.inited && (co.tag == "Player"))
        {
            Transform transform = this.MoveNext();
            this.onRepeat(transform);
            if (this.instances.Count > 5)
            {
                UnityEngine.Object.Destroy(this.instances.Dequeue().gameObject);
            }
        }
    }

    public void SetPrefab(Transform prefab, Vector2 size)
    {
        this.prefab = prefab;
        this.size = size;
    }

    public Vector3 center
    {
        get
        {
            return this.tsm.position;
        }
        set
        {
            this.tsm.position = value;
        }
    }

    public Vector2 direction
    {
        get
        {
            return this.m_direction;
        }
        set
        {
            this.m_direction = value;
        }
    }

    public Transform prefab
    {
        get
        {
            return this.m_prefab;
        }
        set
        {
            this.m_prefab = value;
        }
    }

    public Vector2 size
    {
        get
        {
            return this.trigger.size;
        }
        set
        {
            this.trigger.size = value;
        }
    }

    private BoxCollider2D trigger
    {
        get
        {
            return this.GetTrigger();
        }
    }

    private Transform tsm
    {
        get
        {
            return ((this.m_tsm == null) ? (this.m_tsm = base.GetComponent<Transform>()) : this.m_tsm);
        }
    }
}

